package org.cagt;

import java.util.HashSet;

import org.cagt.exceptions.LimitExceededException;

/**
 * Limited HashSet implementation.
 * 
 * @author konrad.szalkowski (at) gmail.com
 * 
 * @param <T>
 *            Type of elements.
 */
public class LimitedHashSet<T> extends HashSet<T> implements
		LimitedCollection<T> {
	private static final long serialVersionUID = 1L;
	private int limit;

	/**
	 * Default constructor of limited HashSet.
	 * 
	 * @param limit
	 *            HashSets limit.
	 */
	public LimitedHashSet(int limit) {
		super(limit);
		if (limit < 0) {
			throw new IllegalArgumentException("Limit must be greater than 0.");
		}
		this.limit = limit;
	}

	public int getLimit() {
		return limit;
	}

	public boolean add(T object) {
		if (size() + 1 > getLimit()) {
			throw new LimitExceededException(getLimit());
		}
		return super.add(object);
	}

	public boolean isFull() {
		return size() == getLimit();
	}

}
